/**
 * 项目区域 列表页
 *
 * 作者 admin
 * 日期 2024-07-29 15:56:11
 */
layui.config({
    base: ctx + 'febs/'
}).extend({
    commonJS:'lay/extends/common',
    febs: 'lay/modules/febs',
    validate: 'lay/modules/validate'
}).use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-baseProjectArea'),
        $query = $view.find('#query'),
        $add = $view.find('#add'),
        $edit = $view.find('#edit'),
        $delete = $view.find('#delete'),
        $searchForm = $view.find('#baseProjectArea-table-form'),
        tableIns;

    form.verify(validate);
    form.render();

    initTable();

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'baseProjectAreaTable',

            skin: '',
            cols: [[
                {type: 'checkbox'},
                {field: 'projectAreaId', title: '', minWidth: 120},
                {field: 'tenantId', title: '', minWidth: 120},
                {field: 'areaId', title: '', minWidth: 120},
            ]]
        });
        tableIns.reload({url: ctx + 'baseProjectArea/list', where: getQueryParams(), page: {curr: 1}});
    }

    table.on('sort(baseProjectAreaTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    $add.on('click', function () {
        febs.modal.open('新增', 'baseProjectArea/add', {
            btn: ['提交'],
            area: $(window).width() <= 750 ? '90%' : '50%',
            yes: function (index, layero) {
                $('#baseProjectArea-edit').find('#submit').trigger('click');
            },
        });
    });

    $edit.on('click', function () {
        var checkStatus = table.checkStatus('baseProjectAreaTable');
        if (checkStatus.data.length != 1) {
            febs.alert.warn('请选择一条需要修改的数据！');
        } else {
            var id = checkStatus.data[0].projectAreaId;
            febs.modal.open('修改', 'baseProjectArea/update/'+id, {
                btn: ['提交'],
                area: $(window).width() <= 750 ? '90%' : '50%',
                yes: function (index, layero) {
                    $('#baseProjectArea-edit').find('#submit').trigger('click');
                },
            });
        }
    });

    $delete.on('click', function () {
        var checkStatus = table.checkStatus('baseProjectAreaTable');
        if (!checkStatus.data.length) {
            febs.alert.warn('没有选择要删除的数据！');
        } else {
            febs.modal.confirm('删除数据', '确定删除所选数据？', function () {
                var ids = [];
                layui.each(checkStatus.data, function (key, item) {
                    ids.push(item.projectAreaId);
                });
                deleteIds(ids.join(','));
            });
        }
    });

    function getQueryParams() {
        return {
            name: $searchForm.find('input[name="name"]').val()?.trim(),
            invalidate_ie_cache: new Date()
        };
    }

    function deleteIds(ids) {
        febs.get(ctx + 'baseProjectArea/delete/' + ids, null, function () {
            febs.alert.success('删除数据成功');
            $query.trigger('click');
        })
    }




});